import { ObjResettingWithEvent } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { Destroyable, HasOwner, react, reactJson } from "xbsj-xe2/dist-node/xe2-base-utils";
import { MyProjectManager } from "../MyProjectManager";
import { reqGetMonomer, reqGetMonomerProperty, reqUpdateMonomer } from '@/api/modelser2Api';
import { ESGeoExtrudedPolygonUFMS } from '../esobjs/ESGeoExtrudedPolygonUFMS';
import { keysOf } from 'element-plus/es/utils';
import { ElMessage } from 'element-plus';
import { ESClassification } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';


class PickResettting extends Destroyable {
    constructor(monomerEdit: MonomerEdit, owner: MyProjectManager) {
        super();
        const viewer = owner.activeViewer
        if (!viewer) return;

        this.dispose(viewer.clickEvent.don(async e => {
            if (!e.screenPosition) return;
            // 拾取
            const obj = await viewer.pickPosition(e.screenPosition) as [number, number, number]

            // 编辑页面打开
            if (monomerEdit.isShow) return;
            // 单体化信息
            const info = await monomerEdit.getMonomer(obj)

            if (!info || !Object.entries(info).length) {
                // 修改了的单体化属性
                monomerEdit.changedMonomerProperty = {}
                monomerEdit.monomerProperty = []
                monomerEdit.monomerInfo = {}
                monomerEdit.eSClassification.points = []
                return;
            }


            // 单体化属性
            monomerEdit.monomerProperty = monomerEdit.propertyLists.map((item: any) => {
                return {
                    key: item.name,
                    type: item.type,
                    value: info[item.name]
                }
            })

            // 单体化信息
            monomerEdit.monomerInfo = {
                id: info.id,
                name: info.name,
                bottom: info.minh,
                ceilling: info.maxh,
                points: JSON.parse(info.geojson).coordinates[0]
            }

            // 设置es单体化对象 点位 高度
            const points = (JSON.parse(info.geojson).coordinates[0]).slice(0, -1)
            const i = points.findIndex((e: any) => e[2] == Math.min(...points.map((item: any) => item[2])))
            monomerEdit.eSClassification.points = [...points.slice(i), ...points.slice(0, i)]
            monomerEdit.eSClassification.height = info.maxh - info.minh
            monomerEdit.eSClassification.fillStyle = {
                "color": [0, 0, 1, 0.5],
                "material": "",
                "materialParams": {},
                "ground": true
            }

        }))
    }
}

export class MonomerEdit extends HasOwner<MyProjectManager> {

    // 开启拾取
    private _enablePick = this.dv(react<boolean>(false));
    get enablePick() { return this._enablePick.value; }
    get enablePickChanged() { return this._enablePick.changed; }
    set enablePick(value: boolean) { this._enablePick.value = value; }

    // 是否显示编辑页面
    private _isShow = this.dv(react<boolean>(false));
    get isShow() { return this._isShow.value; }
    get isShowChanged() { return this._isShow.changed; }
    set isShow(value: boolean) { this._isShow.value = value; }

    // 单体化信息
    private _monomerInfo = this.dv(reactJson<any>({}));
    get monomerInfo() { return this._monomerInfo.value; }
    get monomerInfoChanged() { return this._monomerInfo.changed; }
    set monomerInfo(value: any) { this._monomerInfo.value = value; }

    // 单体化属性
    private _monomerProperty = this.dv(reactJson<any>([]));
    get monomerProperty() { return this._monomerProperty.value; }
    get monomerPropertyChanged() { return this._monomerProperty.changed; }
    set monomerProperty(value: any) { this._monomerProperty.value = value; }

    // 修改了的单体化属性
    private _changedMonomerProperty = this.dv(reactJson<any>({}));
    get changedMonomerProperty() { return this._changedMonomerProperty.value; }
    get changedMonomerPropertyChanged() { return this._changedMonomerProperty.changed; }
    set changedMonomerProperty(value: any) { this._changedMonomerProperty.value = value; }

    // 所有的单体化属性列表
    private _propertyLists = this.dv(reactJson<any>([]));
    get propertyLists() { return this._propertyLists.value; }
    get propertyListsChanged() { return this._propertyLists.changed; }
    set propertyLists(value: any) { this._propertyLists.value = value; }

    // 单体化
    private _eSClassification = this.dv(this.owner.createSceneObject(ESClassification) as ESClassification);
    get eSClassification() { return this._eSClassification; }

    // 拾取处理
    private _pickResettting = this.dv(new ObjResettingWithEvent(this.enablePickChanged, () => {
        if (!this.enablePick) return undefined;
        return new PickResettting(this, this.owner);
    }))
    get pickResettting() { return this._pickResettting; }

    get eSMsTileset() {
        return this.owner.modelser2Manager.eSMsTileset;
    }

    // 根据经纬度获取单体化
    async getMonomer(position: [number, number, number]) {
        try {
            const param = {
                "tileSerivceName": this.eSMsTileset.tileServiceName,
                "position": position
            }
            const res = await reqGetMonomer(param)
            // @ts-ignore
            if (res.status != 'ok' || !res.data) return [];
            return res.data;


        } catch (error) {
            console.error(error);
        }
    }


    // 获取单体化属性列表
    async getMonomerProperty() {
        try {
            const param = {
                "tileSerivceName": this.eSMsTileset.tileServiceName,
            }
            const res = await reqGetMonomerProperty(param)
            // @ts-ignore
            if (res.status != 'ok' || !res.data) return [];
            this.propertyLists = res.data.data
        } catch (error) {
            console.error(error);
        }
    }



    // 修改单体化
    async updateMonomer(param:any) {
        try {
        
            const res = await reqUpdateMonomer(param)
            // @ts-ignore
            if (res.status != 'ok') return;
            ElMessage({
                message: "修改单体化成功",
                type: 'success',
            })
            this.monomerProperty = []
            this.changedMonomerProperty = {}
            this.monomerInfo = {}
            this.isShow = false
            this.eSClassification.points = []

        } catch (error) {
            console.error(error);
        }
    }

    async init() {
        await this.getMonomerProperty()
        this.enablePick = true
    }
    clear() {
        this.enablePick = false
        this.monomerProperty = []
        this.changedMonomerProperty = {}
        this.monomerInfo = {}
        this.isShow = false
        this.eSClassification.points = []
    }

    constructor(owner: MyProjectManager) {
        super(owner);
    }
}







